package com.jeasonzhao.report.engine.repo;

import java.util.Hashtable;

import com.jeasonzhao.commons.utils.Algorithms;

/**
 * Save all resource initial exceptions here.
 * For example, if the report "demo" has initilized failed, when the customer
 * accessing this report, it will report "the report initial error" rather than
 * "can not found the specified report."
 * The report server would not persistance those information.
 */
public class ExceptionRepository
{
    private static final String DB = "databasekey";
    private static final String DICT = "dictkey";
    private static final String RPT = "reportkey";
    private static final String SEP = "(------<>-----)";

    private static volatile ExceptionRepository m_instance = null;
    private Hashtable<String,Exception> m_hashException = new Hashtable<String,Exception>();
    private ExceptionRepository()
    {
    }

    public static ExceptionRepository getInstance()
    {
        if(null == m_instance)
        {
            synchronized(ExceptionRepository.class)
            {
                m_instance = new ExceptionRepository();
            }
        }
        return m_instance;
    }

    private String generateKey(String strResource,String strId)
    {
        return strResource + SEP + strId;
    }

    public Exception addDatabaseConfigurationException(String strDatabaseId,Exception ex)
    {
        if(null != ex && Algorithms.notEmpty(strDatabaseId))
        {
            m_hashException.put(generateKey(DB,strDatabaseId),ex);
        }
        return ex;
    }

    public Exception addDictConfigurationException(String strDictId,Exception ex)
    {
        if(null != ex && Algorithms.notEmpty(strDictId))
        {
            m_hashException.put(generateKey(DICT,strDictId),ex);
        }

        return ex;
    }

    public Exception addReportException(String strReportId,Exception ex)
    {
        if(null != ex && Algorithms.notEmpty(strReportId))
        {
            m_hashException.put(generateKey(RPT,strReportId),ex);
        }
        return ex;
    }

    public Exception getDictConfigurationException(String strId)
    {
        return m_hashException.get(this.generateKey(DICT,strId));
    }

    public Exception getDatabaseConfigurationException(String strId)
    {
        return m_hashException.get(this.generateKey(DB,strId));
    }

    public Exception addReportException(String strId)
    {
        return m_hashException.get(this.generateKey(RPT,strId));
    }
}
